//
// Created by Administrator on 2022/11/10 0010.
//

#include <onix/thread.h>
#include <onix/mutex.h>
#include <onix/keyboard.h>
#include <onix/task.h>
#include <onix/stdio.h>
#include <onix/onix.h>

#pragma clang diagnostic ignored "-Winvalid-noreturn"
_Noreturn void idle_thread()
{
    set_interrupt_state(TRUE);
    while (TRUE) {
        __asm__ volatile("sti\n"
                         "hlt\n");
        yield();
    }
}

mutex_t mutex;
lock_t lock;

_Noreturn static void user_init_thread()
{
    u32 counter = 0;
    int status;
    while (TRUE) {
    //     pid_t pid = fork();
    //     if (pid) {
    //         printf("fork after parent %d %d %d...\n", pid, getpid(), getppid());
    //         pid_t child = waitpid(pid, &status);
    //         printf("wait pid %d status %d %d\n", child, status, time());
    //     } else {
    //         printf("fork after child %d %d %d...\n", pid, getpid(), getppid());
    //         exit(0);
    //     }
        sleep(10000);
    }
}

void init_thread()
{
    // 为栈顶留有足够的空间
    char temp[100];
    task_to_user_mode(user_init_thread);
}

_Noreturn void test_thread()
{
    set_interrupt_state(TRUE);
    while (TRUE) {
        test();
        sleep(1000);
    }
}